package com.learn.core.controller.learn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LNew;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LNewDTO;
import com.learn.core.service.ILNewService;
import com.learn.core.service.IUserDetailsGatherService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName NewController
 * @date 2021.05.31 09:47
 */
@Api("新闻")
@RestController
@RequestMapping("/learn/new")
public class NewController extends WebController<LNew> {
    @Autowired
    ILNewService ilNewService;
    @Autowired
    IUserDetailsGatherService iUserDetailsGatherService;

    //竞赛模块列表
    @ApiOperation(value = "新闻列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LNew> list(@RequestBody LNewDTO lNewDTO){
        try {
            if(lNewDTO==null||lNewDTO.getPage()==null||lNewDTO.getRows()==null){
                return new PageResult<LNew>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lNewDTO.getPage(),lNewDTO.getRows());
            QueryWrapper<LNew> queryWrapper = new QueryWrapper<LNew>();
            queryWrapper.eq("release_state","1");
            queryWrapper.orderByDesc("new_top","release_time");
            Page<LNew> lNews = (Page<LNew>) ilNewService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lNews)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LNew lNew: lNews) {
                QueryWrapper<UserDetailsGather> gatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
                gatherQueryWrapper.like("user_id",lNew.getCreateUser());
                UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(gatherQueryWrapper);
                if(null != userDetailsGather)
                    lNew.setUserName(userDetailsGather.getName());
            }
            // 返回
            return new PageResult(lNews.getTotal(), lNews, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //详情
    @ApiOperation(value = "详情")
    @GetMapping("/info")
    @UserLoginToken
    public CommonResult<Object> info(@RequestParam("id")Integer id) {
        try {
            LNew lNew = ilNewService.getById(id);
            QueryWrapper<UserDetailsGather> gatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
            gatherQueryWrapper.like("user_id",lNew.getCreateUser());
            UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(gatherQueryWrapper);
            if(null != userDetailsGather)
                lNew.setUserName(userDetailsGather.getName());
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lNew);
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
    }
}
